﻿/*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
 *~*  16-Oct-2009   www.Learning-Leisure.com
 *~*  Main Project : Facebook Membership Provider
 *~*  
 *~*  Project : PB.CustomRoleProvider    Namespace : PB.CustomRoleProvider
 *~*  File    : RoleHelper.cs               Class  : RoleHelper
 *~*  Author : Pinal Bhatt 
 *~*            self@P-Bhatt.com, www.P-Bhatt.com
 *~*   Version History
 *~*            Create Date      :     21-Oct-2009       Pinal Bhatt
 *~*        Freeze Date(Ver 1.0) :     23-Oct-2009       Pinal Bhatt
 *~*         Last Update Date    :     23-Oct-2009       Pinal Bhatt
 *~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*/

using System;
using System.Collections.Generic;
using System.Web.Security;
using System.Linq;

namespace PB.CustomRoleProvider
{
    public class RoleHelper
    {
       
        /// <summary>
        /// Returns comma separated string of roles
        /// </summary>
        /// <param name="rolenNames"></param>
        /// <returns></returns>
        public static string GetStringFromRolesArray(string[] rolenNames)
        {
            string roles = string.Empty;
            foreach (string role in rolenNames)
            {
                if (Roles.RoleExists(role))
                {
                    RoleEnum roleEnum = (RoleEnum)Enum.Parse(typeof(RoleEnum), role);
                    if (roles.Length > 0)
                        roles += ",";
                    roles += ((int)roleEnum).ToString();
                }
            }
            return roles;
        }
        

        internal static string[] GetRolesForUser(string username)
        {
            List<string> roles = DBMapper.GetRolesForUser(CastUserNameToFBUserId(username));
            return roles.ToArray();
        }

        internal static string[] GetUsersInRole(string roleName)
        {
            List<string> userIds = DBMapper.GetUsersInRole(GetRoleEnumFromString(roleName));
            return userIds.ToArray();
        }

        internal static bool IsUserInRole(string username, string roleName)
        {
            string[] rolesForUser = GetRolesForUser(username);
            if (rolesForUser != null
                    && rolesForUser.Length > 0
                    && Array.IndexOf(rolesForUser, roleName) >= 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        internal static void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            foreach (string username in usernames)
            {
                string[] existingRoles = GetRolesForUser(username);
                string[] rolesToRemove = DiffArrays(existingRoles, roleNames);
                if (rolesToRemove != null && rolesToRemove.Length > 0)
                {
                    DBMapper.RemoveRolesForUser(CastUserNameToFBUserId(username), GetStringFromRolesArray(rolesToRemove));
                }
            }   
        }

        internal static void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            foreach (string username in usernames)
            {
                string[] existingRoles = GetRolesForUser(username);
                string[] rolesToAdd = DiffArrays(roleNames, existingRoles);
                if (rolesToAdd != null && rolesToAdd.Length > 0)
                {
                    DBMapper.AddRolesForUser(CastUserNameToFBUserId(username), GetStringFromRolesArray(rolesToAdd));
                }
            }   
        }

        public static RoleEnum GetRoleEnumFromString(string roleName)
        {
            if (!string.IsNullOrEmpty(roleName))
            {
                try
                {
                    if (Enum.IsDefined(typeof(RoleEnum), roleName))
                        return (RoleEnum)Enum.Parse(typeof(RoleEnum), roleName);
                    else
                        throw new ArgumentException("Error at RoleHelper.GetRoleEnumFromString() : Input parameter roleName cannot be casted to RoleEnum");
                }
                catch (Exception ex)
                {
                    throw new Exception("Error at RoleHelper.GetRoleEnumFromString() : " + ex.Message, ex);
                }
            }
            else
            {
                throw new ArgumentNullException("Error at RoleHelper.GetRoleEnumFromString() : Input parameter roleName cannot be null or empty");
            }
        }
        public static long CastUserNameToFBUserId(string username)
        {
            try
            {
                return long.Parse(username);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while casting UserName To FBUserId", ex);
            }
        }



        private static string[] MergeArrays(string[] arr1, string[] arr2)
        {
            List<string> list1 = new List<string>(arr1);
            List<string> list2 = new List<string>(arr2);

            list1.AddRange(from a in list2 where !list1.Contains(a) select a);
            return list1.ToArray();
        }

        private static string[] DiffArrays(string[] arr1, string[] arr2)
        {
            List<string> list1 = new List<string>(arr1);
            List<string> list2 = new List<string>(arr2);

            list1.RemoveAll(x => list2.Contains(x));

            return list1.ToArray();
        }


     
    }
}
